/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validation;

import model.UserBean;

/**
 *
 * @author Shin
 */
public class ValidateForm {

    UserBean validBean = new UserBean();

    public UserBean validateData(UserBean ubean) {
//        boolean isUserValid=false;
//        boolean isDobValid=false;
//        boolean isEmailValid=false;
//        boolean isPhoneValid=false;
//        int terserah=0;
        validBean.setIsValid(true);
        //if given username value is not valid then seterror message and set empty value to input
        //field
        if (ubean.getUserName().length() == 0) {
            validBean.setUserNameError("please enter valid name");
            validBean.setIsValid(false);
            validBean.setUserName("");
        } else if (!ubean.getUserName().matches("[a-zA-Z]{3,15}")) {
            validBean.setUserNameError("please enter valid name");
            validBean.setIsValid(false);
            validBean.setUserName("");

        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setUserNameError("");
            validBean.setUserName(ubean.getUserName());
        }


        //if given username value is not valid then seterror message and set empty value to input
        //field
        if (ubean.getDateOfBirth().length() == 0) {
            validBean.setDateOfBirthError("woi kosong woi tanggale");
            validBean.setIsValid(false);
            validBean.setDateOfBirth(ubean.getDateOfBirth());
        } else if (!ubean.getDateOfBirth().matches("\\d{4}-\\d{1,2}-\\d{1,2}")) {
            validBean.setDateOfBirthError("please enter valid date");
            validBean.setIsValid(false);
            validBean.setDateOfBirth("");

        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setDateOfBirthError("");
            validBean.setDateOfBirth(ubean.getDateOfBirth());
        }


        //if given username value is not valid then seterror message and set empty value to input
        //field
        System.out.println("email" + ubean.getEmail());
        System.out.println(!ubean.getEmail().matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"));
        if (!ubean.getEmail().matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$")) {
            validBean.setEmailError("please enter valid email");
            validBean.setIsValid(false);
            validBean.setEmail("");
        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setEmailError("");
            validBean.setEmail(ubean.getEmail());
        }

        //if given username value is not valid then seterror message and set empty value to input
        //field
        if (!ubean.getPhoneNo().matches("\\d{10}")) {
            validBean.setPhoneNoError("please enter valid phoneno");
            validBean.setIsValid(false);
            validBean.setPhoneNo("");
        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setPhoneNoError("");
            validBean.setPhoneNo(ubean.getPhoneNo());
        }
        
//        if (!ubean.getPass().matches("\\d{10}")) {
//            validBean.setPassError("please enter valid pass");
//            validBean.setIsValid(false);
//            validBean.setPass("");
//        } //if given username value is valid then seterror message to empty and set value to bean
//        else {
//            validBean.setPassError("");
//            validBean.setPass(ubean.getPass());
//            
//    }
       return validBean;
    }
}
